package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupMapper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemDao checkItemDao;
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult page(QueryPageBean queryPageBean) {
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //分页查询数据库
        Page<Setmeal> page = setmealMapper.select(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增
     * @param setmeal
     * @param checkgroupIds
     */
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        Integer setmealId = setmealMapper.insert(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            CheckGroup checkGroup = checkGroupMapper.selectById(checkgroupId);
            String name = checkGroup.getName();
            setmealMapper.insertSetmealGroup(setmealId,checkgroupId,name);
        }
    }

    /**
     * 获取列表展示
     * @return
     */
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmealList = setmealMapper.selectAll();
        return setmealList;
    }

    /**
     * 查看详情
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealMapper.findById(id);

        //获取检查组id
        List<Integer> integers = setmealMapper.getGroupId(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        if(integers != null && integers.size()>0){
            for (Integer integer : integers) {
                //获取检查组信息
                CheckGroup checkGroup = checkGroupMapper.findById(integer);
                List<CheckItem> checkItemList = new ArrayList<>();
                List<Integer> itemIds = checkGroupMapper.findByGroupId(integer);
                if(itemIds != null && itemIds.size() > 0){
                    for (Integer itemId : itemIds) {
                        CheckItem checkItem = checkItemDao.selectById(itemId);
                        checkItemList.add(checkItem);
                    }
                }
                checkGroup.setCheckItems(checkItemList);
                checkGroups.add(checkGroup);
            }
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }
}
